package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.domain.ShopCar;
import cn.wolfcode.trip.base.domain.ShopCarItem;
import cn.wolfcode.trip.base.mapper.ProductMapper;
import cn.wolfcode.trip.base.mapper.ShopCarItemMapper;
import cn.wolfcode.trip.base.mapper.ShopCarMapper;
import cn.wolfcode.trip.base.service.IShopCarService;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShopCarServiceImpl implements IShopCarService {
    @Autowired
    private ShopCarMapper shopCarMapper;
    @Autowired
    private ShopCarItemMapper shopCarItemMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public Map<Long, ShopCarItem> selectForUserId(Long id) {
        ShopCar shopCar = shopCarMapper.selectByPrimaryKey(id);
        if (shopCar.getShopCarItems().size() > 0) {
            // Map集合用 订单项ID , 购物项作为Map的value
            Map<Long, ShopCarItem> mapItem = new HashMap<Long, ShopCarItem>();
            for (ShopCarItem shopCarItem : shopCar.getShopCarItems()) {
                mapItem.put(shopCarItem.getProductId(), shopCarItem);
            }
            shopCar.setShopCarItem(mapItem);
        }
        return shopCar.getShopCarItem();
    }

    @Override
    public void addshopItem(Long productId, Integer count) {
        Map<Long, ShopCarItem> currentUserShopCar = UserContext.getCurrentUserShopCar();
        if(currentUserShopCar.containsKey(productId)){
            // 购物车中已经有购物项
            ShopCarItem item = currentUserShopCar.get(productId);
            Long itemId = item.getId();
            int totalNumber = item.getNumber() + count;
            shopCarItemMapper.updateByPrimaryKey(itemId,totalNumber);
            item.setNumber(totalNumber);
        }else{
            // 购物车中不存在该购物项
            Long userId = UserContext.getCurrentUser().getId();
            Product product = productMapper.selectByPrimaryKey(productId);
            ShopCarItem shopCarItem = new ShopCarItem();
            shopCarItem.setShopCarId(userId);
            shopCarItem.setNumber(count);
            shopCarItem.setProductId(product.getId());
            shopCarItem.setProductName(product.getProductName());
            shopCarItem.setProductPic(product.getCoverUrl());
            shopCarItem.setProductPrice(product.getPrice());
            shopCarItemMapper.insert(shopCarItem);
            currentUserShopCar.put(product.getId(),shopCarItem);
        }
    }
    public void deleteCarItem() {
        Long id = UserContext.getCurrentUser().getId();
        shopCarItemMapper.deleteByPrimaryKey(id);
        //清空购物车
        UserContext.getCurrentUserShopCar().clear();
    }

    @Override
    public void deleteItem(Long productId) {
        ShopCarItem shopCarItem = UserContext.getCurrentUserShopCar().get(productId);
        shopCarItemMapper.deleteItem(shopCarItem.getId());
        UserContext.getCurrentUserShopCar().remove(productId);
    }

    @Override
    public void subProductCount(Long productId) {
        Map<Long, ShopCarItem> currentUserShopCar = UserContext.getCurrentUserShopCar();
        Integer number = currentUserShopCar.get(productId).getNumber();
        if (number>1) {
            Long itemId = currentUserShopCar.get(productId).getId();
            shopCarItemMapper.updateByPrimaryKey(itemId,number-1);
            currentUserShopCar.get(productId).setNumber(number-1);
        }
    }

    @Override
    public List<ShopCarItem> queryProductsByIds(Long [] pids) {
        return shopCarItemMapper.queryProductsByIds(pids,UserContext.getCurrentUser().getId());
    }
}
